﻿using ApprovalFlow.Interface;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ApprovalFlow.Services
{
    public class SingleNodeQueueProvider : IQueueProvider
    {
        private readonly Dictionary<QueueType, BlockingCollection<string>> _queues = new()
        {
            [QueueType.Workflow] = new BlockingCollection<string>(),
            [QueueType.Event] = new BlockingCollection<string>(),
            [QueueType.Index] = new BlockingCollection<string>()
        };

        public Task QueueWork(string id, QueueType queue)
        {
            _queues[queue].Add(id);
            return Task.CompletedTask;
        }

        public Task<string> DequeueWork(QueueType queue, CancellationToken cancellationToken)
        {
            if (_queues[queue].TryTake(out string id, 100, cancellationToken))
                return Task.FromResult(id);
            return Task.FromResult<string>(null);
        }


        public Task Start()
        {
            return Task.CompletedTask;
        }

        public Task Stop()
        {
            return Task.CompletedTask;
        }
    }
}
